package com.xn.app.muying.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xn.app.muying.R;
import com.xn.app.muying.sqlite.DBManager;
import com.xn.app.muying.util.BaseActivity;
import com.xn.app.muying.util.ToastUtil;
import com.xn.app.muying.view.PickerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 温度设置
 * Created by gaoge on 2017/2/10.
 */
public class TemperatureSettingActivity extends BaseActivity implements View.OnClickListener {
    //标题
    private TextView title_name = null;
    //返回
    private ImageView img_back = null;
    //确认按钮
    private ImageView img_right = null;
    //温度选择器
    private PickerView date_picker1 = null;
    private PickerView date_picker2 = null;
    private PickerView date_picker3 = null;
    private PickerView date_picker4 = null;
    private PickerView date_picker5 = null;
    private PickerView date_picker6 = null;
    private PickerView date_picker7 = null;
    private PickerView date_picker8 = null;
    //预设值的发烧温度
    private String number1 = "26.5";
    //预设值的蹬被子温度
    private String number2 = "30.8";
    //预设值的洗澡温度1
    private String number3 = "40.0";
    //预设值的洗澡温度2
    private String number4 = "21.1";
    //数据库
    private DBManager db=null;
    //获取页面传来的数据
    private int type=-1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(R.layout.temperature_setting);
        //初始化组件
        init();
    }

    //初始化
    private void init() {
        db=new DBManager(this);
        type=getIntent().getIntExtra("type",-1);
        title_name = (TextView) findViewById(R.id.title_name);
        img_back = (ImageView) findViewById(R.id.img_back);
        img_right = (ImageView) findViewById(R.id.img_right);
        date_picker1 = (PickerView) findViewById(R.id.date_picker1);
        date_picker2 = (PickerView) findViewById(R.id.date_picker2);
        date_picker3 = (PickerView) findViewById(R.id.date_picker3);
        date_picker4 = (PickerView) findViewById(R.id.date_picker4);
        date_picker5 = (PickerView) findViewById(R.id.date_picker5);
        date_picker6 = (PickerView) findViewById(R.id.date_picker6);
        date_picker7 = (PickerView) findViewById(R.id.date_picker7);
        date_picker8 = (PickerView) findViewById(R.id.date_picker8);
        //设置标题
        title_name.setText("温度设置");
        img_right.setVisibility(ImageView.VISIBLE);
        img_back.setOnClickListener(this);
        img_right.setOnClickListener(this);
        //设置发烧预警温度
        setTemperature(date_picker1, date_picker2, db.selectTemperature("FeverWarning"));
        //设置蹬被子预警温度
        setTemperature(date_picker3, date_picker4,db.selectTemperature("SleepWarning"));
        //设置洗澡上限预警温度
        setTemperature(date_picker5, date_picker6, db.selectTemperature("ShowerWarning").split("-")[0]);
        //设置洗澡下限预警温度
        setTemperature(date_picker7, date_picker8, db.selectTemperature("ShowerWarning").split("-")[1]);
    }

    /**
     * 设置选择的温度
     *
     * @param date_picker1 温度整数
     * @param date_picker2 温度小数
     * @param number       当前选中的温度
     */
    private void setTemperature(PickerView date_picker1, PickerView date_picker2, String number) {
        List<String> data = new ArrayList<>();
        List<String> seconds = new ArrayList<>();
        int oneInt = 0;
        int twoInt = 0;
        String[] str = number.split("\\.");
        for (int i = 0; i < 30; i++) {
            data.add((20 + i) + "");
            if (str[0].equals("" + (20 + i))) {
                oneInt = i;
            }
        }
        for (int i = 0; i < 10; i++) {
            seconds.add("." + i);
            if (str[1].equals("" + i)) {
                twoInt = i;
            }
        }
        date_picker1.setData(data);
        date_picker1.setSelected(oneInt);
        date_picker2.setData(seconds);
        date_picker2.setSelected(twoInt);
    }

    //单击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //返回键
            case R.id.img_back:
                if(type==0){
                    Intent intent = new Intent(TemperatureSettingActivity.this, TemperatureMonitoringActivity.class);
                    startActivity(intent);
                }
                    finish();
                break;
            //确认按钮
            case R.id.img_right:
                if ((date_picker1.getData() + date_picker2.getData()).equals(number1) &&
                        (date_picker3.getData() + date_picker4.getData()).equals(number2) &&
                        (date_picker5.getData() + date_picker6.getData()).equals(number3) &&
                        (date_picker7.getData() + date_picker8.getData()).equals(number4)) {
                    if(type==0){
                        Intent intent = new Intent(TemperatureSettingActivity.this, TemperatureMonitoringActivity.class);
                        startActivity(intent);
                    }
                    finish();
                } else {
                    try {
                        db.updateTemperatureSetAll(date_picker1.getData() + date_picker2.getData(),date_picker3.getData() + date_picker4.getData(),date_picker5.getData() + date_picker6.getData()+"-"+date_picker7.getData() + date_picker8.getData());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    ToastUtil.showToast(TemperatureSettingActivity.this, "温度设置成功");
                    if(type==0){
                        Intent intent = new Intent(TemperatureSettingActivity.this, TemperatureMonitoringActivity.class);
                        startActivity(intent);
                    }
                    finish();
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(type==0){
                Intent intent = new Intent(TemperatureSettingActivity.this, TemperatureMonitoringActivity.class);
                startActivity(intent);
            }
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
